import { heros } from '@config/hero/standard';
import { tr } from '@config/tr';
import { find } from 'lodash-es';
import mitt from 'mitt';
import { reactive, ref } from 'vue';

const DEFAULT_HERO: hero = ['wuming', 'wei', 1, 3, ['1']];
export default function useBasic(): useBasicReturn {
    let hero = DEFAULT_HERO;
    const name = ref(hero[0]);
    const country = ref(hero[1]);
    const sex = ref(hero[2]);
    const initMaxHp = ref(hero[3]);
    const initSkillsConfig = reactive<skillsConfig>([]);
    const eHub = mitt();
    const seat = ref(-1);
    const identity = ref<identity>('unknown');
    const setHeroNum = ref(5);
    const isTurnOut = ref(false);
    const isLinked = ref(false);
    const marks = reactive<marks>({});
    const isCurrent = ref(false);
    const storage = {};

    return {
        name,
        country,
        sex,
        initMaxHp,
        initSkillsConfig,
        seat,
        identity,
        setHeroNum,
        isTurnOut,
        isLinked,
        marks,
        isCurrent,
        storage,
        eHub,
        init(player) {
            hero = find(heros, i => i[0] === player) || DEFAULT_HERO;
            name.value = hero[0];
            country.value = hero[1];
            sex.value = hero[2];
            initMaxHp.value = hero[3];
            initSkillsConfig.splice(0, initSkillsConfig.length, ...hero[4]);
        },
        turnOut() {
            isTurnOut.value = !isTurnOut.value;
            console.log(`${tr(name.value)}已${isTurnOut.value ? '翻面' : '翻回正面'}`);
        },
        link() {
            isLinked.value = !isLinked.value;
            console.log(`${tr(name.value)}已${isLinked.value ? '横置' : '重置'}`);
        },
        addMarks(mark, num = 1) {
            if (mark in marks) {
                marks[mark] += num;
            } else {
                marks[mark] = num;
            }
            console.log(`${tr(name.value)}获得了${num}个${tr(mark)}标记`);
        },
        rmMarks(mark, num = 1) {
            if (mark in marks) {
                marks[mark] -= num;
                console.log(`${tr(name.value)}失去了${num}个${tr(mark)}标记`);
            }
        },
    };
}
// const a: [number, string];
// a[0] = 0;
